{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv\n",
    "import argparse\n",
    "import json\n",
    "from collections import defaultdict, Counter\n",
    "import re\n",
    "\n",
    "MAX_WORDS = 40"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# convert s to snake case\n",
    "def snake_case(s):\n",
    "    return re.sub(\"([a-z])([A-Z])\", \"\\\\1_\\\\2\", s).lower()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''this function splits the key that starts with a given prefix and only for values that are not None\n",
    "and makes the key be the thing after prefix\n",
    "'''\n",
    "def with_prefix(d, prefix):\n",
    "    return {\n",
    "        k.split(prefix)[1]: v\n",
    "        for k, v in d.items()\n",
    "        if k.startswith(prefix) and v not in (\"\", None, \"None\")\n",
    "    }\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "''' this function removes certain prefixes from keys and renames the key to be: key with text following \n",
    "the prefix in the dict'''\n",
    "def remove_key_prefixes(d, ps):\n",
    "    \n",
    "    for p in ps:\n",
    "        d = d.copy()\n",
    "        rm_keys = []\n",
    "        add_items = []\n",
    "        # print(p, d)\n",
    "        for k, v in d.items():\n",
    "            if k.startswith(p):\n",
    "                rm_keys.append(k)\n",
    "                add_items.append((k[len(p) :], v))\n",
    "        for k in rm_keys:\n",
    "            del d[k]\n",
    "        for k, v in add_items:\n",
    "            d[k] = v\n",
    "    return d\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fix_spans_due_to_empty_words(action_dict, words):\n",
    "    \"\"\"Return modified (action_dict, words)\"\"\"\n",
    "\n",
    "    def reduce_span_vals_gte(d, i):\n",
    "        for k, v in d.items():\n",
    "            if type(v) == dict:\n",
    "                reduce_span_vals_gte(v, i)\n",
    "                continue\n",
    "            try:\n",
    "                a, b = v\n",
    "                if a >= i:\n",
    "                    a -= 1\n",
    "                if b >= i:\n",
    "                    b -= 1\n",
    "                d[k] = [a, b]\n",
    "            except ValueError:\n",
    "                pass\n",
    "            except TypeError:\n",
    "                pass\n",
    "\n",
    "    # remove trailing empty strings\n",
    "    while words[-1] == \"\":\n",
    "        del words[-1]\n",
    "\n",
    "    # fix span\n",
    "    i = 0\n",
    "    while i < len(words):\n",
    "        if words[i] == \"\":\n",
    "            reduce_span_vals_gte(action_dict, i)\n",
    "            del words[i]\n",
    "        else:\n",
    "            i += 1\n",
    "\n",
    "    return action_dict, words\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def process_dict(d):\n",
    "    r = {}\n",
    "\n",
    "    # remove key prefixes\n",
    "    # maybe more keys here\n",
    "#     print(d)\n",
    "#     print(\"----------------\")\n",
    "    d = remove_key_prefixes(d, [\"COPY.yes.\", \"COPY.no.\", 'FREEBUILD.BUILD.', 'answer_type.TAG.', 'FREEBUILD.FREEBUILD.', 'coref_resolve_check.yes.', 'coref_resolve_check.no.'])\n",
    "#     print(d)\n",
    "#     print(\"----------------new------------------\")\n",
    "    if \"location\" in d:\n",
    "        # print('yes')\n",
    "        r[\"location\"] = {\"location_type\": d[\"location\"]}\n",
    "        if r['location']['location_type'] == 'coref_resolve_check':\n",
    "            del r['location']['location_type']\n",
    "            # r['location']['coref_resolve'] = d.get(\"location.coref_resolve.coref_resolve\")\n",
    "            # del d[\"location.coref_resolve.coref_resolve\"]\n",
    "        elif r[\"location\"][\"location_type\"] == \"REFERENCE_OBJECT\":\n",
    "            r[\"location\"][\"location_type\"] = \"REFERENCE_OBJECT\"\n",
    "            r[\"location\"][\"relative_direction\"] = d.get(\n",
    "                \"location.REFERENCE_OBJECT.relative_direction\"\n",
    "            )\n",
    "            # no key for EXACT\n",
    "            if r[\"location\"][\"relative_direction\"] in (\"EXACT\", \"Other\"):\n",
    "                del r[\"location\"][\"relative_direction\"]\n",
    "            d[\"location.REFERENCE_OBJECT.relative_direction\"] = None\n",
    "        r[\"location\"].update(process_dict(with_prefix(d, \"location.\")))\n",
    "        \n",
    "    for k, v in d.items():\n",
    "        # print(k, v)\n",
    "        if (\n",
    "            k == \"location\"\n",
    "            or k.startswith(\"location.\")\n",
    "            or k in ['COPY', 'coref_resolve_check']\n",
    "            or (k == \"relative_direction\" and v in (\"EXACT\", \"NEAR\", \"Other\"))\n",
    "        ):\n",
    "            continue\n",
    "        # handle span\n",
    "        if re.match(\"[^.]+.span#[0-9]+\", k):\n",
    "            prefix, rest = k.split(\".\", 1)\n",
    "            idx = int(rest.split(\"#\")[-1])\n",
    "            # print('here')\n",
    "            if prefix in r:\n",
    "                a, b = r[prefix]\n",
    "                r[prefix] = [min(a, idx), max(b, idx)]  # expand span to include idx\n",
    "            else:\n",
    "                r[prefix] = [idx, idx]\n",
    "\n",
    "        # handle nested dict\n",
    "        elif \".\" in k:\n",
    "            prefix, rest = k.split(\".\", 1)\n",
    "            prefix_snake = snake_case(prefix)\n",
    "            r[prefix_snake] = r.get(prefix_snake, {})\n",
    "            r[prefix_snake].update(process_dict(with_prefix(d, prefix + \".\")))\n",
    "\n",
    "        # handle const value\n",
    "        else:\n",
    "            r[k] = v\n",
    "\n",
    "    return r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def process_repeat_dict(d):\n",
    "    if d[\"loop\"] == \"ntimes\":\n",
    "        repeat_dict = {\"repeat_key\": \"FOR\"}\n",
    "        processed_d = process_dict(with_prefix(d, \"loop.ntimes.\"))\n",
    "        if 'repeat_for' in processed_d:\n",
    "            repeat_dict[\"repeat_count\"] = processed_d[\"repeat_for\"]\n",
    "        if 'repeat_dir' in processed_d:\n",
    "            repeat_dict['repeat_dir'] = processed_d['repeat_dir']\n",
    "        return repeat_dict\n",
    "    if d[\"loop\"] == \"repeat_all\":\n",
    "        repeat_dict = {\"repeat_key\": \"ALL\"}\n",
    "        processed_d = process_dict(with_prefix(d, \"loop.repeat_all.\"))\n",
    "        if 'repeat_dir' in processed_d:\n",
    "            repeat_dict['repeat_dir'] = processed_d['repeat_dir']\n",
    "        return repeat_dict\n",
    "    if d[\"loop\"] == \"forever\":\n",
    "        return {\"stop_condition\": {\"condition_type\": \"NEVER\"}}\n",
    "    if d['loop'] == 'repeat_until':\n",
    "        stripped_d = with_prefix(d, 'loop.repeat_until.')\n",
    "        processed_d = process_dict(stripped_d)\n",
    "        if 'adjacent_to_block_type' in processed_d:\n",
    "            return {\"stop_condition\" : {\n",
    "                        \"condition_type\" : 'ADJACENT_TO_BLOCK_TYPE',\n",
    "                        'block_type': processed_d['adjacent_to_block_type']}\n",
    "                   }\n",
    "    raise NotImplementedError(\"Bad repeat dict option: {}\".format(d[\"loop\"]))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def remove_prefix(text, prefix):\n",
    "    if text.startswith(prefix):\n",
    "        return text[len(prefix):]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "def process_get_memory_dict(d):\n",
    "    filters_val = d['filters']\n",
    "    out_dict = {'filters': {}}\n",
    "    parent_dict = {}\n",
    "    if filters_val.startswith('type.'):\n",
    "        parts = remove_prefix(filters_val, 'type.').split('.')\n",
    "        type_val = parts[0]\n",
    "        if type_val in ['ACTION', 'AGENT']:\n",
    "            out_dict['filters']['temporal'] = 'CURRENT'\n",
    "            tag_val = parts[1]\n",
    "            out_dict['answer_type'] = 'TAG'\n",
    "            out_dict['tag_name'] = parts[1] # the name of tag is here\n",
    "            if type_val == 'ACTION':\n",
    "                x = with_prefix(d, 'filters.'+filters_val+'.')\n",
    "                out_dict['filters'].update(x)\n",
    "        elif type_val in ['REFERENCE_OBJECT']:\n",
    "            d.pop('filters')\n",
    "            ref_obj_dict = remove_key_prefixes(d, ['filters.type.'])\n",
    "            ref_dict = process_dict(ref_obj_dict)\n",
    "            if 'answer_type' in ref_dict['reference_object']:\n",
    "                out_dict['answer_type'] = ref_dict['reference_object']['answer_type']\n",
    "                ref_dict['reference_object'].pop('answer_type')\n",
    "            if 'tag_name' in ref_dict['reference_object']:\n",
    "                out_dict['tag_name'] = ref_dict['reference_object']['tag_name']\n",
    "                ref_dict['reference_object'].pop('tag_name')    \n",
    "            out_dict['filters'].update(ref_dict)\n",
    "            \n",
    "        out_dict['filters']['type'] = type_val\n",
    "        \n",
    "    return out_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "def handle_get_memory(d):\n",
    "    out_d = {'dialogue_type': 'GET_MEMORY'}\n",
    "    child_d = process_get_memory_dict(with_prefix(d, \"action_type.ANSWER.\"))\n",
    "    out_d.update(child_d)\n",
    "    return out_d\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "def handle_put_memory(d):\n",
    "    return {}\n",
    "    \n",
    "\n",
    "def handle_commands(d):\n",
    "    action_name = d[\"action_type\"]\n",
    "    child_d = process_dict(with_prefix(d, \"action_type.{}.\".format(action_name)))\n",
    "    \n",
    "    # Fix Build/Freebuild mismatch\n",
    "    if child_d.get(\"FREEBUILD\") == \"FREEBUILD\":\n",
    "        action_name = 'FREEBUILD'\n",
    "    child_d.pop(\"FREEBUILD\", None)\n",
    "    action_dict = {'action_type': action_name}\n",
    "    action_dict.update(child_d)\n",
    "    d = {'dialogue_type': 'HUMAN_GIVE_COMMAND',\n",
    "                    'action_sequence': [\n",
    "                            action_dict\n",
    "                    ]}\n",
    "    return d\n",
    "\n",
    "def process_result(full_d):\n",
    "    worker_id = full_d[\"WorkerId\"]\n",
    "    print(full_d)\n",
    "    d = with_prefix(full_d, \"Answer.root.\")\n",
    "    print(d)\n",
    "    try:\n",
    "        action = d[\"action_type\"]\n",
    "    except KeyError:\n",
    "        return None, None, None\n",
    "    # fix this for new format:\n",
    "    print(action)\n",
    "    if action == 'TAG':\n",
    "        print(\"1\")\n",
    "        action_dict = handle_put_memory(d)\n",
    "    elif action == 'ANSWER':\n",
    "        print(\"2\")\n",
    "        action_dict = handle_get_memory(d)\n",
    "    else:\n",
    "        print(\"3\")\n",
    "        action_dict = handle_commands(d)\n",
    "        \n",
    "    # action_dict = {action: process_dict(with_prefix(d, \"action_type.{}.\".format(action)))}\n",
    "\n",
    "    ##############\n",
    "    # repeat dict\n",
    "    ##############\n",
    "    #NOTE: this can probably loop over or hold indices of which specific action ?\n",
    "    if action_dict['dialogue_type'] == 'HUMAN_GIVE_COMMAND':\n",
    "        action_specific_dict = action_dict['action_sequence'][0]\n",
    "        if d.get(\"loop\") not in [None, \"Other\"]:\n",
    "            repeat_dict = process_repeat_dict(d)\n",
    "            # Some turkers annotate a repeat dict for a repeat_count of 1.\n",
    "            # Don't include the repeat dict if that's the case\n",
    "            if repeat_dict.get(\"repeat_count\"):\n",
    "                a, b = repeat_dict[\"repeat_count\"]\n",
    "                repeat_count_str = \" \".join(\n",
    "                    [full_d[\"Input.word{}\".format(x)] for x in range(a, b + 1)]\n",
    "                )\n",
    "                if repeat_count_str not in (\"a\", \"an\", \"one\", \"1\"):\n",
    "                    action_val = list(action_dict.values())[0]  # check what this is\n",
    "                    if action_specific_dict.get(\"schematic\"):\n",
    "                        action_specific_dict[\"schematic\"][\"repeat\"] = repeat_dict\n",
    "                    elif action_specific_dict.get(\"reference_object\"):\n",
    "                        action_specific_dict[\"reference_object\"][\"repeat\"] = repeat_dict\n",
    "                    else:\n",
    "                        action_specific_dict[\"repeat\"] = repeat_dict\n",
    "            elif 'stop_condition' in repeat_dict:\n",
    "                action_specific_dict.update(repeat_dict)\n",
    "    elif action_dict['dialogue_type'] == 'GET_MEMORY':\n",
    "        action_dict =  action_dict\n",
    "    ##################\n",
    "    # post-processing\n",
    "    ##################\n",
    "\n",
    "   \n",
    "\n",
    "    # Fix empty words messing up spans\n",
    "    words = [full_d[\"Input.word{}\".format(x)] for x in range(MAX_WORDS)]\n",
    "    action_dict, words = fix_spans_due_to_empty_words(action_dict, words)\n",
    "\n",
    "    return worker_id, action_dict, words\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OrderedDict([('HITId', '3SD15I2WD4XYX00L7P9WDI6H3HQ63O'), ('HITTypeId', '33EWSTAVY770TGQSCAXMENQ36KPWVF'), ('Title', 'Sentence Meaning Questionnaire'), ('Description', 'Answer a series of multiple-choice questions relating to the meaning of a sentence'), ('Keywords', 'sentence, annotation, questionnaire, tagging'), ('Reward', '$0.30'), ('CreationTime', 'Mon Oct 14 15:23:37 PDT 2019'), ('MaxAssignments', '1'), ('RequesterAnnotation', 'BatchId:254252;OriginalHitTemplateId:920937243;'), ('AssignmentDurationInSeconds', '300'), ('AutoApprovalDelayInSeconds', '259200'), ('Expiration', 'Thu Oct 17 15:23:37 PDT 2019'), ('NumberOfSimilarHITs', ''), ('LifetimeInSeconds', ''), ('AssignmentId', '374TNBHA8DYVA5EM2MLMX1F1HIBQYV'), ('WorkerId', 'A3M3ZFDVYMBJ1X'), ('AssignmentStatus', 'Submitted'), ('AcceptTime', 'Mon Oct 14 15:33:11 PDT 2019'), ('SubmitTime', 'Mon Oct 14 15:33:37 PDT 2019'), ('AutoApprovalTime', 'Thu Oct 17 15:33:37 PDT 2019'), ('ApprovalTime', ''), ('RejectionTime', ''), ('RequesterFeedback', ''), ('WorkTimeInSeconds', '26'), ('LifetimeApprovalRate', '70% (16/23)'), ('Last30DaysApprovalRate', '70% (16/23)'), ('Last7DaysApprovalRate', '70% (16/23)'), ('Input.command', 'where are you going'), ('Input.word0', 'where'), ('Input.word1', 'are'), ('Input.word2', 'you'), ('Input.word3', 'going'), ('Input.word4', ''), ('Input.word5', ''), ('Input.word6', ''), ('Input.word7', ''), ('Input.word8', ''), ('Input.word9', ''), ('Input.word10', ''), ('Input.word11', ''), ('Input.word12', ''), ('Input.word13', ''), ('Input.word14', ''), ('Input.word15', ''), ('Input.word16', ''), ('Input.word17', ''), ('Input.word18', ''), ('Input.word19', ''), ('Input.word20', ''), ('Input.word21', ''), ('Input.word22', ''), ('Input.word23', ''), ('Input.word24', ''), ('Input.word25', ''), ('Input.word26', ''), ('Input.word27', ''), ('Input.word28', ''), ('Input.word29', ''), ('Answer.root.action_type', 'ANSWER'), ('Answer.root.action_type.ANSWER.filters', 'type.AGENT.move_target'), ('Answer.root.action_type.ANSWER.filters.type.ACTION.action_reference_object_name.action_type', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.answer_type', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.answer_type.TAG.tag_name', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.coref_resolve_check', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.coref_resolve_check.no.has_name.span#2', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.coref_resolve_check.no.has_name.span#4', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.has_colour.span#3', ''), ('Approve', None), ('Reject', None)])\n",
      "{'action_type': 'ANSWER', 'action_type.ANSWER.filters': 'type.AGENT.move_target'}\n",
      "ANSWER\n",
      "2\n",
      "['where', 'are', 'you', 'going']\n",
      "{'answer_type': 'TAG',\n",
      " 'dialogue_type': 'GET_MEMORY',\n",
      " 'filters': {'temporal': 'CURRENT', 'type': 'AGENT'},\n",
      " 'tag_name': 'move_target'}\n",
      "--------------------\n",
      "OrderedDict([('HITId', '3X2LT8FDHYLISCLXAOHW2TD575AW86'), ('HITTypeId', '33EWSTAVY770TGQSCAXMENQ36KPWVF'), ('Title', 'Sentence Meaning Questionnaire'), ('Description', 'Answer a series of multiple-choice questions relating to the meaning of a sentence'), ('Keywords', 'sentence, annotation, questionnaire, tagging'), ('Reward', '$0.30'), ('CreationTime', 'Mon Oct 14 15:23:37 PDT 2019'), ('MaxAssignments', '1'), ('RequesterAnnotation', 'BatchId:254252;OriginalHitTemplateId:920937243;'), ('AssignmentDurationInSeconds', '300'), ('AutoApprovalDelayInSeconds', '259200'), ('Expiration', 'Thu Oct 17 15:23:37 PDT 2019'), ('NumberOfSimilarHITs', ''), ('LifetimeInSeconds', ''), ('AssignmentId', '3VW04L3ZLV9QJXPOJ4UC8V4LCF4XXT'), ('WorkerId', 'A3M3ZFDVYMBJ1X'), ('AssignmentStatus', 'Submitted'), ('AcceptTime', 'Mon Oct 14 15:34:44 PDT 2019'), ('SubmitTime', 'Mon Oct 14 15:34:52 PDT 2019'), ('AutoApprovalTime', 'Thu Oct 17 15:34:52 PDT 2019'), ('ApprovalTime', ''), ('RejectionTime', ''), ('RequesterFeedback', ''), ('WorkTimeInSeconds', '8'), ('LifetimeApprovalRate', '70% (16/23)'), ('Last30DaysApprovalRate', '70% (16/23)'), ('Last7DaysApprovalRate', '70% (16/23)'), ('Input.command', 'where are you'), ('Input.word0', 'where'), ('Input.word1', 'are'), ('Input.word2', 'you'), ('Input.word3', ''), ('Input.word4', ''), ('Input.word5', ''), ('Input.word6', ''), ('Input.word7', ''), ('Input.word8', ''), ('Input.word9', ''), ('Input.word10', ''), ('Input.word11', ''), ('Input.word12', ''), ('Input.word13', ''), ('Input.word14', ''), ('Input.word15', ''), ('Input.word16', ''), ('Input.word17', ''), ('Input.word18', ''), ('Input.word19', ''), ('Input.word20', ''), ('Input.word21', ''), ('Input.word22', ''), ('Input.word23', ''), ('Input.word24', ''), ('Input.word25', ''), ('Input.word26', ''), ('Input.word27', ''), ('Input.word28', ''), ('Input.word29', ''), ('Answer.root.action_type', 'ANSWER'), ('Answer.root.action_type.ANSWER.filters', 'type.AGENT.location'), ('Answer.root.action_type.ANSWER.filters.type.ACTION.action_reference_object_name.action_type', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.answer_type', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.answer_type.TAG.tag_name', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.coref_resolve_check', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.coref_resolve_check.no.has_name.span#2', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.coref_resolve_check.no.has_name.span#4', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.has_colour.span#3', ''), ('Approve', None), ('Reject', None)])\n",
      "{'action_type': 'ANSWER', 'action_type.ANSWER.filters': 'type.AGENT.location'}\n",
      "ANSWER\n",
      "2\n",
      "['where', 'are', 'you']\n",
      "{'answer_type': 'TAG',\n",
      " 'dialogue_type': 'GET_MEMORY',\n",
      " 'filters': {'temporal': 'CURRENT', 'type': 'AGENT'},\n",
      " 'tag_name': 'location'}\n",
      "--------------------\n",
      "OrderedDict([('HITId', '32LAQ1JNTBSOYSOGIMDQB1OV02KTUT'), ('HITTypeId', '33EWSTAVY770TGQSCAXMENQ36KPWVF'), ('Title', 'Sentence Meaning Questionnaire'), ('Description', 'Answer a series of multiple-choice questions relating to the meaning of a sentence'), ('Keywords', 'sentence, annotation, questionnaire, tagging'), ('Reward', '$0.30'), ('CreationTime', 'Mon Oct 14 15:23:37 PDT 2019'), ('MaxAssignments', '1'), ('RequesterAnnotation', 'BatchId:254252;OriginalHitTemplateId:920937243;'), ('AssignmentDurationInSeconds', '300'), ('AutoApprovalDelayInSeconds', '259200'), ('Expiration', 'Thu Oct 17 15:23:37 PDT 2019'), ('NumberOfSimilarHITs', ''), ('LifetimeInSeconds', ''), ('AssignmentId', '3PEIJLRY6VWBUXKY9ZYQ6CQE3DYXW0'), ('WorkerId', 'A3M3ZFDVYMBJ1X'), ('AssignmentStatus', 'Submitted'), ('AcceptTime', 'Mon Oct 14 15:33:39 PDT 2019'), ('SubmitTime', 'Mon Oct 14 15:33:50 PDT 2019'), ('AutoApprovalTime', 'Thu Oct 17 15:33:50 PDT 2019'), ('ApprovalTime', ''), ('RejectionTime', ''), ('RequesterFeedback', ''), ('WorkTimeInSeconds', '11'), ('LifetimeApprovalRate', '70% (16/23)'), ('Last30DaysApprovalRate', '70% (16/23)'), ('Last7DaysApprovalRate', '70% (16/23)'), ('Input.command', 'what are you doing'), ('Input.word0', 'what'), ('Input.word1', 'are'), ('Input.word2', 'you'), ('Input.word3', 'doing'), ('Input.word4', ''), ('Input.word5', ''), ('Input.word6', ''), ('Input.word7', ''), ('Input.word8', ''), ('Input.word9', ''), ('Input.word10', ''), ('Input.word11', ''), ('Input.word12', ''), ('Input.word13', ''), ('Input.word14', ''), ('Input.word15', ''), ('Input.word16', ''), ('Input.word17', ''), ('Input.word18', ''), ('Input.word19', ''), ('Input.word20', ''), ('Input.word21', ''), ('Input.word22', ''), ('Input.word23', ''), ('Input.word24', ''), ('Input.word25', ''), ('Input.word26', ''), ('Input.word27', ''), ('Input.word28', ''), ('Input.word29', ''), ('Answer.root.action_type', 'ANSWER'), ('Answer.root.action_type.ANSWER.filters', 'type.ACTION.action_name'), ('Answer.root.action_type.ANSWER.filters.type.ACTION.action_reference_object_name.action_type', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.answer_type', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.answer_type.TAG.tag_name', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.coref_resolve_check', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.coref_resolve_check.no.has_name.span#2', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.coref_resolve_check.no.has_name.span#4', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.has_colour.span#3', ''), ('Approve', None), ('Reject', None)])\n",
      "{'action_type': 'ANSWER', 'action_type.ANSWER.filters': 'type.ACTION.action_name'}\n",
      "ANSWER\n",
      "2\n",
      "['what', 'are', 'you', 'doing']\n",
      "{'answer_type': 'TAG',\n",
      " 'dialogue_type': 'GET_MEMORY',\n",
      " 'filters': {'temporal': 'CURRENT', 'type': 'ACTION'},\n",
      " 'tag_name': 'action_name'}\n",
      "--------------------\n",
      "OrderedDict([('HITId', '3WGCNLZJKHB1861LU83RVV1CMJZ1DJ'), ('HITTypeId', '33EWSTAVY770TGQSCAXMENQ36KPWVF'), ('Title', 'Sentence Meaning Questionnaire'), ('Description', 'Answer a series of multiple-choice questions relating to the meaning of a sentence'), ('Keywords', 'sentence, annotation, questionnaire, tagging'), ('Reward', '$0.30'), ('CreationTime', 'Mon Oct 14 15:23:37 PDT 2019'), ('MaxAssignments', '1'), ('RequesterAnnotation', 'BatchId:254252;OriginalHitTemplateId:920937243;'), ('AssignmentDurationInSeconds', '300'), ('AutoApprovalDelayInSeconds', '259200'), ('Expiration', 'Thu Oct 17 15:23:37 PDT 2019'), ('NumberOfSimilarHITs', ''), ('LifetimeInSeconds', ''), ('AssignmentId', '3F6HPJW4JF3ATHH1LADXGMWIQ4XW2O'), ('WorkerId', 'A3M3ZFDVYMBJ1X'), ('AssignmentStatus', 'Submitted'), ('AcceptTime', 'Mon Oct 14 15:34:54 PDT 2019'), ('SubmitTime', 'Mon Oct 14 15:35:03 PDT 2019'), ('AutoApprovalTime', 'Thu Oct 17 15:35:03 PDT 2019'), ('ApprovalTime', ''), ('RejectionTime', ''), ('RequesterFeedback', ''), ('WorkTimeInSeconds', '9'), ('LifetimeApprovalRate', '70% (16/23)'), ('Last30DaysApprovalRate', '70% (16/23)'), ('Last7DaysApprovalRate', '70% (16/23)'), ('Input.command', 'what are you building'), ('Input.word0', 'what'), ('Input.word1', 'are'), ('Input.word2', 'you'), ('Input.word3', 'building'), ('Input.word4', ''), ('Input.word5', ''), ('Input.word6', ''), ('Input.word7', ''), ('Input.word8', ''), ('Input.word9', ''), ('Input.word10', ''), ('Input.word11', ''), ('Input.word12', ''), ('Input.word13', ''), ('Input.word14', ''), ('Input.word15', ''), ('Input.word16', ''), ('Input.word17', ''), ('Input.word18', ''), ('Input.word19', ''), ('Input.word20', ''), ('Input.word21', ''), ('Input.word22', ''), ('Input.word23', ''), ('Input.word24', ''), ('Input.word25', ''), ('Input.word26', ''), ('Input.word27', ''), ('Input.word28', ''), ('Input.word29', ''), ('Answer.root.action_type', 'ANSWER'), ('Answer.root.action_type.ANSWER.filters', 'type.ACTION.action_reference_object_name'), ('Answer.root.action_type.ANSWER.filters.type.ACTION.action_reference_object_name.action_type', 'BUILD'), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.answer_type', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.answer_type.TAG.tag_name', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.coref_resolve_check', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.coref_resolve_check.no.has_name.span#2', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.coref_resolve_check.no.has_name.span#4', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.has_colour.span#3', ''), ('Approve', None), ('Reject', None)])\n",
      "{'action_type': 'ANSWER', 'action_type.ANSWER.filters': 'type.ACTION.action_reference_object_name', 'action_type.ANSWER.filters.type.ACTION.action_reference_object_name.action_type': 'BUILD'}\n",
      "ANSWER\n",
      "2\n",
      "['what', 'are', 'you', 'building']\n",
      "{'answer_type': 'TAG',\n",
      " 'dialogue_type': 'GET_MEMORY',\n",
      " 'filters': {'action_type': 'BUILD', 'temporal': 'CURRENT', 'type': 'ACTION'},\n",
      " 'tag_name': 'action_reference_object_name'}\n",
      "--------------------\n",
      "OrderedDict([('HITId', '3VGET1QSZ22ESYGE0JPQOTHAWL4W7J'), ('HITTypeId', '33EWSTAVY770TGQSCAXMENQ36KPWVF'), ('Title', 'Sentence Meaning Questionnaire'), ('Description', 'Answer a series of multiple-choice questions relating to the meaning of a sentence'), ('Keywords', 'sentence, annotation, questionnaire, tagging'), ('Reward', '$0.30'), ('CreationTime', 'Mon Oct 14 15:23:37 PDT 2019'), ('MaxAssignments', '1'), ('RequesterAnnotation', 'BatchId:254252;OriginalHitTemplateId:920937243;'), ('AssignmentDurationInSeconds', '300'), ('AutoApprovalDelayInSeconds', '259200'), ('Expiration', 'Thu Oct 17 15:23:37 PDT 2019'), ('NumberOfSimilarHITs', ''), ('LifetimeInSeconds', ''), ('AssignmentId', '351SEKWQS2K1RFL3EXRR37LVPT8MDW'), ('WorkerId', 'A3M3ZFDVYMBJ1X'), ('AssignmentStatus', 'Submitted'), ('AcceptTime', 'Mon Oct 14 15:35:05 PDT 2019'), ('SubmitTime', 'Mon Oct 14 15:36:14 PDT 2019'), ('AutoApprovalTime', 'Thu Oct 17 15:36:14 PDT 2019'), ('ApprovalTime', ''), ('RejectionTime', ''), ('RequesterFeedback', ''), ('WorkTimeInSeconds', '69'), ('LifetimeApprovalRate', '70% (16/23)'), ('Last30DaysApprovalRate', '70% (16/23)'), ('Last7DaysApprovalRate', '70% (16/23)'), ('Input.command', 'is the house red'), ('Input.word0', 'is'), ('Input.word1', 'the'), ('Input.word2', 'house'), ('Input.word3', 'red'), ('Input.word4', ''), ('Input.word5', ''), ('Input.word6', ''), ('Input.word7', ''), ('Input.word8', ''), ('Input.word9', ''), ('Input.word10', ''), ('Input.word11', ''), ('Input.word12', ''), ('Input.word13', ''), ('Input.word14', ''), ('Input.word15', ''), ('Input.word16', ''), ('Input.word17', ''), ('Input.word18', ''), ('Input.word19', ''), ('Input.word20', ''), ('Input.word21', ''), ('Input.word22', ''), ('Input.word23', ''), ('Input.word24', ''), ('Input.word25', ''), ('Input.word26', ''), ('Input.word27', ''), ('Input.word28', ''), ('Input.word29', ''), ('Answer.root.action_type', 'ANSWER'), ('Answer.root.action_type.ANSWER.filters', 'type.REFERENCE_OBJECT'), ('Answer.root.action_type.ANSWER.filters.type.ACTION.action_reference_object_name.action_type', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.answer_type', 'EXISTS'), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.answer_type.TAG.tag_name', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.coref_resolve_check', 'no'), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.coref_resolve_check.no.has_name.span#2', 'on'), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.coref_resolve_check.no.has_name.span#4', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.has_colour.span#3', 'on'), ('Approve', None), ('Reject', None)])\n",
      "{'action_type': 'ANSWER', 'action_type.ANSWER.filters': 'type.REFERENCE_OBJECT', 'action_type.ANSWER.filters.type.REFERENCE_OBJECT.answer_type': 'EXISTS', 'action_type.ANSWER.filters.type.REFERENCE_OBJECT.coref_resolve_check': 'no', 'action_type.ANSWER.filters.type.REFERENCE_OBJECT.coref_resolve_check.no.has_name.span#2': 'on', 'action_type.ANSWER.filters.type.REFERENCE_OBJECT.has_colour.span#3': 'on'}\n",
      "ANSWER\n",
      "2\n",
      "['is', 'the', 'house', 'red']\n",
      "{'answer_type': 'EXISTS',\n",
      " 'dialogue_type': 'GET_MEMORY',\n",
      " 'filters': {'reference_object': {'has_colour': [3, 3], 'has_name': [2, 2]},\n",
      "             'type': 'REFERENCE_OBJECT'}}\n",
      "--------------------\n",
      "OrderedDict([('HITId', '3H5TOKO3DBMJFWJP73EFNNYNHHM649'), ('HITTypeId', '33EWSTAVY770TGQSCAXMENQ36KPWVF'), ('Title', 'Sentence Meaning Questionnaire'), ('Description', 'Answer a series of multiple-choice questions relating to the meaning of a sentence'), ('Keywords', 'sentence, annotation, questionnaire, tagging'), ('Reward', '$0.30'), ('CreationTime', 'Mon Oct 14 15:23:37 PDT 2019'), ('MaxAssignments', '1'), ('RequesterAnnotation', 'BatchId:254252;OriginalHitTemplateId:920937243;'), ('AssignmentDurationInSeconds', '300'), ('AutoApprovalDelayInSeconds', '259200'), ('Expiration', 'Thu Oct 17 15:23:37 PDT 2019'), ('NumberOfSimilarHITs', ''), ('LifetimeInSeconds', ''), ('AssignmentId', '3TMSXRD2X8334WZ526DJB3MDYYJW17'), ('WorkerId', 'A3M3ZFDVYMBJ1X'), ('AssignmentStatus', 'Submitted'), ('AcceptTime', 'Mon Oct 14 15:33:53 PDT 2019'), ('SubmitTime', 'Mon Oct 14 15:34:41 PDT 2019'), ('AutoApprovalTime', 'Thu Oct 17 15:34:41 PDT 2019'), ('ApprovalTime', ''), ('RejectionTime', ''), ('RequesterFeedback', ''), ('WorkTimeInSeconds', '48'), ('LifetimeApprovalRate', '70% (16/23)'), ('Last30DaysApprovalRate', '70% (16/23)'), ('Last7DaysApprovalRate', '70% (16/23)'), ('Input.command', 'how big is the cube'), ('Input.word0', 'how'), ('Input.word1', 'big'), ('Input.word2', 'is'), ('Input.word3', 'the'), ('Input.word4', 'cube'), ('Input.word5', ''), ('Input.word6', ''), ('Input.word7', ''), ('Input.word8', ''), ('Input.word9', ''), ('Input.word10', ''), ('Input.word11', ''), ('Input.word12', ''), ('Input.word13', ''), ('Input.word14', ''), ('Input.word15', ''), ('Input.word16', ''), ('Input.word17', ''), ('Input.word18', ''), ('Input.word19', ''), ('Input.word20', ''), ('Input.word21', ''), ('Input.word22', ''), ('Input.word23', ''), ('Input.word24', ''), ('Input.word25', ''), ('Input.word26', ''), ('Input.word27', ''), ('Input.word28', ''), ('Input.word29', ''), ('Answer.root.action_type', 'ANSWER'), ('Answer.root.action_type.ANSWER.filters', 'type.REFERENCE_OBJECT'), ('Answer.root.action_type.ANSWER.filters.type.ACTION.action_reference_object_name.action_type', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.answer_type', 'TAG'), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.answer_type.TAG.tag_name', 'has_size'), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.coref_resolve_check', 'no'), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.coref_resolve_check.no.has_name.span#2', ''), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.coref_resolve_check.no.has_name.span#4', 'on'), ('Answer.root.action_type.ANSWER.filters.type.REFERENCE_OBJECT.has_colour.span#3', ''), ('Approve', None), ('Reject', None)])\n",
      "{'action_type': 'ANSWER', 'action_type.ANSWER.filters': 'type.REFERENCE_OBJECT', 'action_type.ANSWER.filters.type.REFERENCE_OBJECT.answer_type': 'TAG', 'action_type.ANSWER.filters.type.REFERENCE_OBJECT.answer_type.TAG.tag_name': 'has_size', 'action_type.ANSWER.filters.type.REFERENCE_OBJECT.coref_resolve_check': 'no', 'action_type.ANSWER.filters.type.REFERENCE_OBJECT.coref_resolve_check.no.has_name.span#4': 'on'}\n",
      "ANSWER\n",
      "2\n",
      "['how', 'big', 'is', 'the', 'cube']\n",
      "{'answer_type': 'TAG',\n",
      " 'dialogue_type': 'GET_MEMORY',\n",
      " 'filters': {'reference_object': {'has_name': [4, 4]},\n",
      "             'type': 'REFERENCE_OBJECT'},\n",
      " 'tag_name': 'has_size'}\n",
      "--------------------\n"
     ]
    }
   ],
   "source": [
    "result_counts = defaultdict(Counter)  # map[command] -> Counter(dict)\n",
    "from pprint import pprint\n",
    "'''\n",
    "command: Input.command\n",
    "\n",
    "'''\n",
    "example_d = {}\n",
    "with open('/Users/kavyasrinet/Downloads/Ovt_14_answer.csv', \"r\") as f:\n",
    "    r = csv.DictReader(f)\n",
    "    for i, d in enumerate(r):\n",
    "        sentence = d['Input.command']\n",
    "        worker_id, action_dict, words = process_result(d)\n",
    "        print(words)\n",
    "        pprint(action_dict)\n",
    "        print(\"----\"*5)\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
